<?php 
    session_start(); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="stylesheet" href="../style/users.css" />

<?php 
    include_once("../global.php");
    include_once ("config.php");
    
    if(isset($_POST["done"])){
        if (($_FILES["avatar"]["type"] == "")||($_FILES["avatar"]["type"] == "image/gif")|| ($_FILES["avatar"]["type"] == "image/jpeg")|| ($_FILES["avatar"]["type"] == "image/png")|| ($_FILES["avatar"]["type"] == "image/jpg"))
        {
            
		$folder = "../images/avatar/"; // folder upload
		$dir = $folder.$_FILES["avatar"]["name"]; // duong dan upload
		move_uploaded_file($_FILES["avatar"]["tmp_name"],$dir);
		$_SESSION["avatar"] = "".$_FILES["avatar"]["name"];
        
        $id = $_SESSION['id'];
        $avatar = $_SESSION["avatar"];
        $sql = "UPDATE vt_register SET avatar='$avatar' WHERE id=$id";
        
        $re = mysql_query($sql); 
        if($re) {
        ?>
            <script>
                alert("Upload thành công");
                window.location.href="users_index.php";
            </script>
        
        <?php }
        else {
            echo "please choose image jpg, jpeg, gif, png";
        }
    }
}
?>
<form action="users_chavatar.php" method="post" enctype="multipart/form-data">
<div> 
    Chọn avatar mới: <br /> 
    <input type="file" name="avatar" id="avatar" />
    <input type="submit" name="done" />
</div>
</form>